package com.bee.common.core.log.service.impl;

import com.bee.common.core.log.persistence.dao.LogExceptionDao;
import com.bee.common.core.log.persistence.entity.LogException;
import com.bee.common.core.log.persistence.entity.LogRequest;
import com.bee.common.core.log.service.ILogExceptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;

/**
 * Created by liekkas on 2017-8-1.
 */
@Service
public class LogExceptionServiceImpl implements ILogExceptionService {

    private Logger log = LoggerFactory.getLogger(LogRequestServiceImp.class);

    private BlockingQueue<LogException> basket = new ArrayBlockingQueue(2000);

    @Autowired
    private LogExceptionDao logExceptionDao;

    @Override
    public void addLogExceptionAsyn(LogException logException) {
        basket.offer(logException);
    }


    @PostConstruct
    public void excuteAsyn(){

        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                while(true){
                    LogException notify;
                    try {
                        notify = basket.take();
                        logExceptionDao.save(notify);
                    }catch(Exception e){
                        log.error("错误信息",e);
                    }
                }
            }
        });
    }
}
